package com.gxa.eshop.portal.controller;

import com.gxa.eshop.common.Result;
import com.gxa.eshop.exception.BusinessException;
import com.gxa.eshop.portal.thread.UserThreadLocal;
import com.gxa.eshop.service.CartService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author bill
 * @date 2022/1/20 11:30
 */
@RequestMapping("/cart")
@RestController
public class CartController {

    @Reference
    private CartService cartService;


    @PostMapping(value = "/add")
    public Result addGoodsToCart(String skuId, Integer num) throws BusinessException {

        int i = 10 ;
        cartService.addGoodsToCart(UserThreadLocal.get().getId(),skuId,num);
        return new Result();
    }

    @PostMapping(value = "/update")
    public Result updateCartItemNum(String skuId,Integer num){
        cartService.updateCartItemNum(UserThreadLocal.get().getId(),skuId,num);
        return new Result();
    }

    @PostMapping(value = "/removeCartItem")
    public Result removeCartItem(String skuId){
        cartService.removeCartItem(UserThreadLocal.get().getId(),skuId);
        return new Result();
    }

    @PostMapping(value = "/clearCart")
    public Result clearCart(){
        cartService.clearCart(UserThreadLocal.get().getId());
        return new Result();
    }

    @PostMapping("/queryUserCart")
    public Result queryUserCart(){
        Map result = cartService.queryUserCart(UserThreadLocal.get().getId());
        return new Result(result);
    }

}
